﻿using System;
using System.Runtime.Serialization;
using System.Transactions;

namespace RockBus.DataContracts
{
    [DataContract(Namespace = StringConstants.RockBusNamespace)]
    public class EndpointInfo
    {
        public EndpointInfo()
        {
            this.EndpointName = EndpointNameEnum.BasicHttpEndpoint;
            this.ExchangeType = ExchangeTypeEnum.TwoWayAck;
            this.TransactionScopeOption = TransactionScopeOption.Suppress;
        }

        private EndpointInfo(string id, string address, ExchangeTypeEnum exchangeType = ExchangeTypeEnum.TwoWayAck, TransactionScopeOption transactionScopeOption = TransactionScopeOption.Suppress, TransportTypeEnum transportType = TransportTypeEnum.WcfTransport)
        {
            this.Id = id;
            this.Address = address;
            this.ExchangeType = exchangeType;
            this.TransactionScopeOption = transactionScopeOption;
            this.TransportType = transportType;
        }

        public EndpointInfo(string id, string address, string endpointName, ExchangeTypeEnum exchangeType = ExchangeTypeEnum.TwoWayAck, TransactionScopeOption transactionScopeOption = TransactionScopeOption.Suppress, TransportTypeEnum transportType = TransportTypeEnum.WcfTransport)
            : this(id, address, exchangeType, transactionScopeOption, transportType)
        {
            EndpointNameEnum endpointNameEnum;
            if (Enum.TryParse<EndpointNameEnum>(endpointName, out endpointNameEnum))
            {
                this.EndpointName = endpointNameEnum;
            }
            else
            {
                this.EndpointName = EndpointNameEnum.Custom;
                this.CustomEndpointName = endpointName;
            }
        }

        public EndpointInfo(string id, string address, EndpointNameEnum endpointName, ExchangeTypeEnum exchangeType = ExchangeTypeEnum.TwoWayAck, TransactionScopeOption transactionScopeOption = TransactionScopeOption.Suppress, TransportTypeEnum transportType = TransportTypeEnum.WcfTransport)
            : this(id, address, exchangeType, transactionScopeOption, transportType)
        {
            this.EndpointName = endpointName;
        }

        public EndpointInfo(EndpointInfo other)
            : this(other.Id, other.Address, other.ExchangeType, other.TransactionScopeOption, other.TransportType)
        {
            this.EndpointName = other.EndpointName;
            this.CustomEndpointName = other.CustomEndpointName;
            this.AddRoutingHeader = other.AddRoutingHeader;
        }

        [DataMember]
        public string Id { get; set; }

        [DataMember]
        public ExchangeTypeEnum ExchangeType { get; set; }

        [DataMember]
        public TransportTypeEnum TransportType { get; set; }

        [DataMember]
        public EndpointNameEnum EndpointName { get; set; }

        [DataMember]
        public string CustomEndpointName { get; set; }

        [DataMember]
        public string Address { get; set; }

        [DataMember]
        public TransactionScopeOption TransactionScopeOption { get; set; }

        [DataMember]
        public bool AddRoutingHeader { get; set; }

        public string EndpointConfigurationName
        {
            get
            {
                return (this.EndpointName == EndpointNameEnum.Custom) ? this.CustomEndpointName : this.EndpointName.ToString();
            }
        }

        public string TransportName
        {
            get
            {
                return this.TransportType.ToString();
            }
        }

        public bool IsTransactional
        {
            get
            {
                return (this.TransactionScopeOption != TransactionScopeOption.Suppress);
            }
        }

        public bool IsCorrelated
        {
            get
            {
                return (this.ExchangeType == ExchangeTypeEnum.OneWayRequestResponse) || (this.ExchangeType == ExchangeTypeEnum.TwoWayAckRequestResponse);
            }
        }

        public override string ToString()
        {
            return string.Format("EndpointInfo: {0}", this.Address);
        }
    }
}